package com.his.controller;

import com.his.annotation.EnableEncryption;
import com.his.annotation.RequireRoles;
import com.his.common.Const;
import com.his.common.PageParam;
import com.his.common.Rsp;
import com.his.common.User;
import com.his.entity.HisUser;
import com.his.entity.Patient;
import com.his.service.HisUserService;
import com.his.utils.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * @author dawn
 */
@RestController
@RequestMapping("user")
@Validated
@Slf4j
public class UserController {
    @Resource
    private HisUserService userService;

    /**
     * @param token 前端传过来的token
     * @return 返回解析后的用户参数
     * @throws Exception 解析异常
     */
    @GetMapping("getUserInfo")
    public Rsp getUserInfo(@NotBlank(message = "token不能为空") String token) throws Exception {
        Object object = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        if (object instanceof HisUser) {
            HisUser hisUser = (HisUser) object;
            return Rsp.success(new User(hisUser.getRealName(), hisUser.getRoles()));
        }
        Patient p = (Patient) object;
        return Rsp.success(new User(p.getRealName(), Arrays.asList("patient")));
    }

    /**
     * @param user 前端传过来的用户参数
     * @return 返回登录状态
     * @throws Exception 生成token异常
     */
    @EnableEncryption
    @PostMapping("login")
    public Rsp login(@Validated({Const.LoginGroup.class}) @RequestBody HisUser user) throws Exception {
        HisUser login = userService.login(user);
        if (login == null) {
            return Rsp.fail().msg("用户名或者密码错误");
        }
        if ("1".equals(login.getStatus())) {
            return Rsp.fail().msg("账号已被锁定,请联系管理员");
        }
        return Rsp.success(JWTUtil.generateToken(login, 30, Const.PRIVATE_KEY));
    }

    @PostMapping("logout")
    public Rsp logout() {
        return Rsp.success(null).msg("成功").token("null");
    }

    /**
     * 获取用户信息
     *
     * @param pageParam 查询条件
     * @return 返回获取的用户
     */
    @RequireRoles(roles = {"hr", "admin"}, index = 0)
    @PostMapping("searchSomeUserByPage")
    public Rsp searchSomeUserByPage(@RequestHeader(Const.TOKEN) String token, @RequestBody PageParam<HisUser> pageParam) {
        return Rsp.success(userService.queryAll(pageParam));
    }

    @RequireRoles(roles = {"hr", "admin"}, index = 0)
    @PostMapping("addOneUser")
    public Rsp addOneUser(@RequestHeader(Const.TOKEN) String token, @Validated @RequestBody HisUser user) {
        return userService.insert(user);
    }

    /**
     * 更新账号状态
     *
     * @param token 检验参数
     * @param user  前端传递的参数
     * @return 修改状态
     */
    @RequireRoles(roles = {"hr", "admin"}, index = 0)
    @PostMapping("updateStatus")
    public Rsp updateStatus(@RequestHeader(Const.TOKEN) String token, @Validated({Const.UpdateUserStatus.class}) @RequestBody HisUser user) {
        return userService.update(user);
    }

    /**
     * 更新用户
     *
     * @param token 检验参数
     * @param user  前端传递的参数
     * @return 修改状态
     */
    @RequireRoles(roles = {"hr", "admin"}, index = 0)
    @PostMapping("updateUser")
    public Rsp updateUser(@RequestHeader(Const.TOKEN) String token, @RequestBody HisUser user) {
        return userService.update(user);
    }

    /**
     * 根据部门id查询用户信息
     *
     * @param token 检验参数
     * @param id    部门id
     * @return 返回给前端数据
     */
    @RequireRoles(roles = {"hr", "admin"}, index = 0)
    @GetMapping("getUserByDepId")
    public Rsp getUserByDepId(@RequestHeader(Const.TOKEN) String token, @NotNull(message = "部门id不能为空") Integer id) {
        return userService.getByDepId(id);
    }
}
